package scene.map 
{
	import event.CommEvent;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.getTimer;
	import log.Log;
	import resource.ImageResource;
	/**
	 * ...
	 * @author carlor
	 */
	public class MapTile 
	{
		private var _owner	: Map;				
		private var _imageFileName: String;		//Tile图片文件名
		private var _image	: ImageResource;
		private var _pos	: Point;
		private var _rect 	: Rectangle;		//Tile图片在场景中的位置
		private var _loading: Boolean;			//是否在加载
		
		private var _useTime: Number;
		
		public function MapTile(owner: Map, imageFileName: String, col: int, row: int, rect: Rectangle) 
		{
			_owner = owner;
			_imageFileName = imageFileName;
			_pos = new Point(col, row);
			_rect = rect;
			_loading = false;
		}
		
		public function dispose(): void
		{
			this.unLoadImage();
			_rect = null;
		}
		
		public function isLoaded(): Boolean
		{
			if (_image)
				return _image.isLoaded;
			else if (! _loading) {
				loadImage();
				return false;
			}else
				return false;
		}
		
		public function loadImage(): void
		{
			_loading = true;
			Engine.resourceManager.loadResource(_imageFileName, ImageResource, onLoaded, onLoadFailed);
		}
		
		public function unLoadImage(): void
		{
			if (_image != null) {
				Engine.resourceManager.unloadResource(_imageFileName, ImageResource);
				_image = null;
				_loading = false;
			}
		}
		
		private function onLoaded(img: ImageResource): void
		{
			_image = img;
			_loading = false;
			_owner.dispatchEvent(new CommEvent(CommEvent.LOADED_COMPLETE).addData("mapTile", this));
		}
		
		private function onLoadFailed(img: ImageResource): void
		{
			_image = null;
			_loading = false;
			Log.formatErrorLog("MapTile.onLoadFailed ImageFile=%%", [_imageFileName]);
		}
		
		public function get image(): ImageResource
		{
			return _image;
		}
		
		public function get rect(): Rectangle
		{
			return _rect;
		}
		
		public function get pos(): Point
		{
			return _pos;
		}
	}

}